// pages/compare_checked/index/index.js
const {
  schoolList,
  schoolAdd,
} = require('../../../api/api')
const {
  checkLogin
} = require('../../../utils/util')
function filterArr(selected, selectData) {
  let c = selectData.filter((item) => {
    return (selected.indexOf(item.id) > -1)
  })
  return c
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    list: [],
    currentRegion: {
      name: '全国',
      code: 0
    },
    showSelect: false,
    type: {
      name: '本科',
      id: 2,
    },
    pageNo: 1,
    loginVisible: true,
    pageSize: 20,
    bottomLift: getApp().globalData.bottomLift,
    compareList: [],
    key: 'add',
    checkedList: [],
    length: 6,
    checkedIds: [],
    isAllChecked: false,
    schoolName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.params) {
      const compareList = JSON.parse(decodeURIComponent(options.params))
      compareList.map((item) => {
        item.isChecked = false
      })
      this.setData({
        compareList,
      })
    }
    
  },
  inputChange(e){
    this.setData({
      schoolName:e.detail.value
    })
  },

  onShow: function () {

  },

  deleteItem(e) {
    const {
      id
    } = e.currentTarget.dataset
    const {
      compareList,
      checkedIds,
      checkedList
    } = this.data
    compareList.map((item, index) => {
      if (item.id == id) {
        compareList.splice(index, 1)
      }
    })
    checkedList.map((item, index) => {
      if (item.id == id) {
        checkedList.splice(index, 1)
      }
    })
    checkedIds.splice(checkedIds.indexOf(id), 1)
    this.setData({
      compareList,
      checkedList,
      checkedIds
    })
  },

  compare() {
    const {
      checkedIds,
      checkedList
    } = this.data
    if (checkedIds.length < 2) {
      wx.showToast({
        title: '至少选择两个学校',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: '/pages/school/compare_detail/index?params=' + encodeURIComponent(JSON.stringify(checkedList)),
    })
  },
  checkAll(e) {
    var {
      compareList,
      checkedList,
      checkedIds,
      isAllChecked
    } = this.data
    // console.log(isAllChecked)
    if (isAllChecked) {
      compareList.map((item) => {
        item.isChecked = false
      })
      checkedList = []
      checkedIds = []
    } else {
      let len = 6 - checkedIds.length
      compareList.map((item) => {
        if (len < 1 || checkedIds.indexOf(item.id) > -1) return
        checkedList.push(item)
        len -= 1
        item.isChecked = true
      })
      checkedIds = checkedList.map((item) => {
        return item.id
      })
    }
    this.setData({
      compareList,
      isAllChecked: !isAllChecked,
      checkedIds,
      checkedList
    })
  },
  checkItem(e) {
    const {
      index,
      checked,
      id
    } = e.currentTarget.dataset
    const {
      compareList,
      checkedList,
      checkedIds
    } = this.data
    if (checked) {
      // 删除
      compareList[index].isChecked = !checked
      const aIndex = checkedIds.indexOf(id)
      checkedIds.splice(aIndex, 1)
      checkedList.splice(aIndex, 1)
    } else {
      //添加
      if (checkedIds.length > 5) {
        wx.showToast({
          title: '不能再选择更多了哦',
          icon: 'none'
        })
        return
      }
      checkedIds.push(id)
      checkedList.push(compareList[index])
      compareList[index].isChecked = !checked
    }

    this.setData({
      compareList,
      checkedIds,
      checkedList,
      isAllChecked: checkedIds.length == compareList.length
    })
  },

})